<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Admin\Lianggoods;
use App\Admin\Daren;
use App\Admin\Categoods;
use App\Admin\Label;
use App\Admin\GetCategory;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class LabelController extends Controller
{
    //标签列表显示
    public function labelshow(Request $request){
        if($request->search){
            $search = $request->search;
            $searchlist =Label::where('name','like','%'.$search.'%');
            $result=$searchlist->paginate(5);
        }else{
            $Sing = Label::where([])->orderBy('id','asc');
            $result = $Sing->paginate(5);
        }
        return view('admin/label/LabelList',compact('result'));
    }
    //添加标签
    public function labeladd(){
        return view('admin/label/LabelAdd');
    }
    public function labeladddata(Request $request){
        //验证规则
        $roles = [
            'name'=>'required|between:1,10',
        ];
        //自定义的错误信息
        $msg = [
            'name.required' => '单品名称不能为空',
            'name.between'=>'单品名称必须在:min和:max之间',
        ];
        $validator = Validator::make($request->all(), $roles, $msg);
        if ($validator->fails()) {
            //验证失败
            return redirect('admin/LabelAdd')->withErrors($validator);
        }
        $input = $request->all();
        $res = Label::create($input);
        return redirect('admin/LabelAdd');
    }
    //修改标签
    public function labeledit(){
        $search = isset($_GET['search'])?$_GET['search']:'';
        $page = $_GET['page'];
        $id = $_GET['id'];
        $Label = Label::where('id','=',$id)->get();
        return view('admin/label/LabelEdit',['Label'=>$Label,'page'=>$page,'search'=>$search]);
    }
    public function labeleditdata(Request $request,$page,$search=''){
        //接收要修改的ID
        $id = $request->id;
        //验证规则
        $roles = [
            'name'=>'required|between:1,10',
        ];
        //自定义的错误信息
        $msg = [
            'name.required' => '单品名称不能为空',
            'name.between'=>'单品名称必须在:min和:max之间',
        ];
        $validator = Validator::make($request->all(), $roles, $msg);
        if ($validator->fails()) {
            //验证失败
            return redirect('admin/LabelEdit?id='.$id.'&page='.$page)->withErrors($validator);
        }else{

                $input = Input::except('_token');
                $res = Label::where('id',$id)->update($input);
                return redirect('admin/LabelList?page='.$page.'&search='.$search);

        }
    }
//    删除单品
    public function labeldel(Request $request){
        $search=$request->search;
        $id = $_GET['id'];
        $page=$_GET['page'];
        $Label = Label::find($id);
        $SingList = $Label->delete();
        if($search==''){
            $count = DB::table('label')->count();
//            dd($count);
        }else{
            $count = DB::table('label')
                ->where('name','like','%'.$search.'%')
                ->count();
        }
        $num = $count%5;
        if($num==0){
            $page= $page-1;
        }
        return redirect('admin/LabelList?page='.$page.'&search='.$search);
    }
}
